package servlet;

import bean.User;
import service.LoginTransferService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginTransferServlet")
public class LoginTransferServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        接收表单数据
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);
        String Password = request.getParameter("Password");

//        判空
        if (( idStr == null || idStr == "") ||(Password == null || Password == "") ) {
            response.getWriter().write("银行卡号或者密码不能为空");
        } else {
//        封装成对象
        User user = new User();
        user.setUserName(idStr);
        user.setPassword(Password);
//        调用业务层处理
            LoginTransferService login = new LoginTransferService();
            boolean result = login.isOk(user);
            response.setContentType("text/html;charset=UTF-8");
            if (result == false) {
                response.getWriter().write("登录失败，请重新登录");
            } else {
                response.getWriter().write("登录成功");
                //        跳转到 Transfer.jsp
                request.setAttribute("id", idStr);
                getServletContext().getRequestDispatcher("/html1/Transfer.jsp").forward(
                        request, response);
            }
        }
    }
}
